package com.todaynews.ui.smallvideo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.core.view.animation.PathInterpolatorCompat;

import com.todaynews.R;
import com.todaynews.frame.model.InfoResult;
import com.todaynews.frame.ui.activity.BaseActivity;
import com.todaynews.ui.smallvideo.transition.StatusBarUtils;
import com.todaynews.ui.smallvideo.transition.TransitionCallback;
import com.todaynews.ui.smallvideo.transition.TransitionController;
import com.todaynews.ui.smallvideo.transition.TransitionParam;
import com.todaynews.ui.smallvideo.transition.TransitionUtils;
import butterknife.BindView;

public class VideoPlayActivity extends BaseActivity {

    @BindView(R.id.video_cover)
    ImageView videoCover;
    @BindView(R.id.main_root_layer)
    FrameLayout mainRootLayer;

    private static final String ANIM_PARAM = "ANIM_PARAM";
    /**
     * 外部控件位置参数
     */
    private TransitionParam targetAnimBean;
    private TransitionController transitionController;

    public static void intentStart(Context context, TransitionParam animBean) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra(ANIM_PARAM, animBean);
        context.startActivity(intent);
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        StatusBarUtils.enableStatusBar(this, true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_video_play;
    }

    @Override
    public void init() {
        setupView();
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (transitionController != null) {
            transitionController.transitionRelease();
        }
    }

    private void setupView() {

        targetAnimBean = getIntent().getParcelableExtra(ANIM_PARAM);

        transitionController = new TransitionController.Builder()
                .with(findViewById(R.id.main_root_layer))
                .setInterpolator(PathInterpolatorCompat.create(0.32F, 0.94F, 0.6F, 1.0F))
                .duration(320)
                .build();
        transitionController.transitionEnter(targetAnimBean, new TransitionCallback() {
            @Override
            public void onTransitionStop() {

            }
        });
    }

    @Override
    public void onBackPressed() {
        if (targetAnimBean != null) {
            transitionController.transitionExit(new TransitionCallback() {
                @Override
                public void onTransitionStop() {
                    finish();
                }
            });
        } else {
            finish();
        }
    }

    @Override
    public void finish() {
        super.finish();
        TransitionUtils.finishTransition(this);
    }
}
